﻿using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Business.ApartmentServices;
using blackhouse.BlackApart.Business.Reservations;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace blackhouse.StareKino.QuartzActions.ASyncMails {
    public partial class ReservationDetailTable : System.Web.UI.UserControl {

        public IWebReservation Reservation { get; set; }
        public string CurrencyCode { get; set; }
        private Factory factory = Factory.Instance();
        private ReservationAdditionalServicesOrder servicesOrder;

        #region Methods

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.servicesOrder = new ReservationAdditionalServicesOrder(this.Reservation);

            this.rptReservationDetails.DataSource = this.Reservation.ApartmentReservation;
            this.rptReservationDetails.DataBind();

            IEnumerable<ReservationAdditionalService> reservationServices = this.servicesOrder.GetOrderingReservationServices();
            if (reservationServices.Count() > 0) {
                this.rptReservationServices.DataSource = reservationServices;
                this.rptReservationServices.DataBind();
            }
            else
                this.rptReservationServices.Visible = false;

            this.PopulateReservationSummary();
        }

        internal static string GetFormattedPrice(double price, string currencyCode) {
            if (String.IsNullOrEmpty(currencyCode) || currencyCode.Equals("pln", StringComparison.InvariantCultureIgnoreCase))
                return String.Format(CultureInfo.GetCultureInfo("pl-PL"), "{0:C}", price);
            else
                return new Currencies.Currency(price, "PLN", 2).ToCurrency(currencyCode).ToString();
        }

        private string GetFormattedPrice(double price) {
            return GetFormattedPrice(price, this.CurrencyCode);
        }


        private void PopulateReservationSummary() {
            this.ltlReservationPriceWithoutRabat.Text = this.GetFormattedPrice(this.servicesOrder.Value + this.Reservation.Price);
            this.ltlReservationRabat.Text = String.Format("{0}%", this.Reservation.Rabat);
            this.ltlReservationFinalPrice.Text = this.GetFormattedPrice(this.servicesOrder.Value + this.Reservation.FinalPrice);
            if (this.Reservation.PaymentType != null) {
                this.phPaymentType.Visible = true;
                this.ltlPaymentType.Text = this.Reservation.PaymentType.Name;
            }
        }

        protected void rptReservationDetails_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            switch (e.Item.ItemType) {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    ApartmentWebReservation reservationItem = e.Item.DataItem as ApartmentWebReservation;
                    if (reservationItem != null) {
                        HyperLink lnkToApartment = e.Item.FindControl("lnkToApartment") as HyperLink;
                        Literal ltlPricePerNight = e.Item.FindControl("ltlPricePerNight") as Literal;
                        Literal ltlPriceWithoutRabat = e.Item.FindControl("ltlPriceWithoutRabat") as Literal;
                        Literal ltlRabat = e.Item.FindControl("ltlRabat") as Literal;
                        Literal ltlFinalPrice = e.Item.FindControl("ltlFinalPrice") as Literal;
                        Literal ltlLengthOfStay = e.Item.FindControl("ltlLengthOfStay") as Literal;

                        if (lnkToApartment != null) {
                            IApartmentGroup group = this.factory.GetApartment(reservationItem.ApartmentId).Group;
                            lnkToApartment.NavigateUrl = blackhouse.mojoPortalExtend.PageProvider.AdjustUrlToCurrentSite(group.Url);
                            lnkToApartment.Text = group.Name;
                        }
                        if (ltlPricePerNight != null)
                            ltlPricePerNight.Text = this.GetFormattedPrice(reservationItem.FinalPrice / (this.Reservation.DepartureDate.Date - this.Reservation.ArrivalDate.Date).TotalDays);
                        if (ltlPriceWithoutRabat != null)
                            ltlPriceWithoutRabat.Text = this.GetFormattedPrice(reservationItem.Price);
                        if (ltlRabat != null)
                            ltlRabat.Text = String.Format("{0:F0}%", this.Reservation.Rabat);
                        if (ltlFinalPrice != null)
                            ltlFinalPrice.Text = this.GetFormattedPrice(reservationItem.FinalPrice);
                        if (ltlLengthOfStay != null)
                            ltlLengthOfStay.Text = this.Reservation.PrintLegthOfStay();
                    }
                    else
                        e.Item.Visible = false;
                    break;

            }
        }

        protected void rptReservationServices_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            switch (e.Item.ItemType) {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    ReservationAdditionalService reservationService = e.Item.DataItem as ReservationAdditionalService;
                    if (reservationService != null) {
                        Literal ltlPricePerNight = e.Item.FindControl("ltlPricePerNight") as Literal;
                        Literal ltlPriceWithoutRabat = e.Item.FindControl("ltlPriceWithoutRabat") as Literal;
                        Literal ltlFinalPrice = e.Item.FindControl("ltlFinalPrice") as Literal;
                        Literal ltlQuantity = e.Item.FindControl("ltlQuantity") as Literal;

                        if (ltlPricePerNight != null)
                            ltlPricePerNight.Text = this.GetFormattedPrice(reservationService.Price / reservationService.Quantity);
                        if (ltlPriceWithoutRabat != null)
                            ltlPriceWithoutRabat.Text = this.GetFormattedPrice(reservationService.Price);
                        if (ltlFinalPrice != null)
                            ltlFinalPrice.Text = this.GetFormattedPrice(reservationService.Price);
                        if (ltlQuantity != null)
                            ltlQuantity.Text = reservationService.Quantity.ToString();

                    }
                    break;
            }
        }

        #endregion

    }
}